package com.example;

import android.arch.lifecycle.LifecycleActivity;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.example.arch.MyLifeCycle;
import com.example.arch.MyLiveData;
import com.example.arch.UserViewModel;
import com.example.arch.bean.User;
import com.example.arch.room.DbHelper;
import com.example.zhangyongqing.myapplication.R;
import io.reactivex.schedulers.Schedulers;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import java.util.List;

public class MainActivity extends LifecycleActivity {

    private TextView textView;
    private UserViewModel model;

    private MyLiveData myLiveData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.textView);

        //ViewModel
        model = ViewModelProviders.of(this).get(UserViewModel.class);

        UserViewModel mm = ViewModelProviders.of(this).get(UserViewModel.class);

        //两个对象是同一个对象
        Log.e("ss", "  "+model+ " /"+mm);

        model.getUser().observe(this, new Observer<User>() {
            @Override
            public void onChanged(@Nullable final User user) {
                textView.setText(String.format("%s:%d", user.name, user.age));
            }
        });


        findViewById(R.id.btn_change).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                User user = new User();
                user.name = "adsf";
                user.age = 200;
                model.setLiveUser(user);
            }
        });

        //lifecycle
        this.getLifecycle().addObserver(new MyLifeCycle());

        //livedata
        myLiveData = new MyLiveData();
        Log.e("ss", "has :"+myLiveData.hasObservers());
        //false
        Log.e("ss", "has active:"+myLiveData.hasActiveObservers());
        myLiveData.observe(this, new Observer<User>() {
            @Override
            public void onChanged(@Nullable User user) {

            }
        });
        myLiveData.observe(this, new Observer<User>() {
            @Override
            public void onChanged(@Nullable User user) {

            }
        });

        Log.e("ss", "len:"+myLiveData.hasObservers());

        //room Db
        testRoom();
    }

    private void testRoom() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                DbHelper.getInstance().getDb().userDao().insertAll(new User());
                Log.e("s", "load from db:"+DbHelper.getInstance().getDb().userDao().getAll().size());
            }
        }).start();

        DbHelper.getInstance().getDb().userDao().loadAll()
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.single())
                .subscribe(new Subscriber<List<User>>() {
                    @Override
                    public void onSubscribe(Subscription s) {
                        s.request(2);
                    }

                    @Override
                    public void onNext(List<User> users) {
                        Log.e("s", "rx load from db:"+users.size());

                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {
                        Log.e("s", "rx load from db finished");
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();

        //true
        Log.e("ss", "onresume has active:"+myLiveData.hasActiveObservers());

    }
}
